package com.ljx.funchat.chat.dao;

import com.ljx.funchat.chat.domain.entity.RoomGroup;
import com.ljx.funchat.chat.domain.entity.RoomGroupMember;
import com.ljx.funchat.chat.domain.vo.req.GroupMemberPageReq;
import com.ljx.funchat.chat.mapper.RoomGroupMemberMapper;
import com.ljx.funchat.chat.service.IRoomGroupMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.funchat.common.domain.vo.resp.CursorPageBaseResp;
import com.ljx.funchat.common.utils.CursorUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiJunXi
 * @since 2024-02-14
 */
@Service
public class RoomGroupMemberDao extends ServiceImpl<RoomGroupMemberMapper, RoomGroupMember> {

    public RoomGroupMember getByGroupId(Long groupId, Long uid) {
        return lambdaQuery().eq(RoomGroupMember::getGroupId, groupId)
                .eq(RoomGroupMember::getUid, uid)
                .one();
    }

    public List<Long> getMemberUidList(Long groupId) {
        return lambdaQuery()
                .eq(RoomGroupMember::getGroupId, groupId)
                .list().stream()
                .map(RoomGroupMember::getUid)
                .collect(Collectors.toList());
    }

    public CursorPageBaseResp<RoomGroupMember> getMemberPage(GroupMemberPageReq req, RoomGroup roomGroup) {
        return CursorUtils.getCursorPageByMysql(this, req, wrapper -> {
            wrapper.eq(RoomGroupMember::getGroupId, roomGroup.getId())
                    .orderByAsc(RoomGroupMember::getRole);
        }, RoomGroupMember::getId);
    }

    public boolean countIsJoin(Long uid, Long targetId) {
        Integer count = lambdaQuery().eq(RoomGroupMember::getUid, uid)
                .eq(RoomGroupMember::getGroupId, targetId)
                .count();
        return count > 0;
    }
}
